package com.xiaoq.shop.endpoint;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xiaoq.apis.user.IUserApi;
import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericListResponse;
import com.xiaoq.shop.entity.CouponEntity;
import com.xiaoq.shop.service.ICouponService;
import com.xiaoq.shop.vo.CouponVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping(value = "/api/v1/coupons")
public class CouponEndpoint2C {

    private static final Logger LOGGER = LoggerFactory.getLogger(CouponEndpoint2C.class);
    private static final class CouponVOListResponse extends GenericListResponse<CouponVO> {}
    private static final class CouponVOResponse extends GenericDataResponse<CouponVO> {}


    @Autowired
    private ICouponService couponService;

    @Reference(interfaceClass=IUserApi.class, check=false, version="1.0.0")
    private IUserApi userDubboApi;

    @ApiOperation(value="查询优惠券列表", notes="查询优惠券列表")
    @ApiResponses({
            @ApiResponse(code = 200, response = CouponVOListResponse.class, message = "查询优惠券列表")
    })
    @GetMapping(value="/{shopId}", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> couponList(HttpServletRequest request,
                                        @PathVariable("shopId") @ApiParam(value="店(景区)的Id", required=true) String shopId) {

        String userId = (String) request.getAttribute("userId");
        List<CouponVO> couponVOList = couponService.getAvailableCouponList(shopId);
        return CouponVOListResponse.listAndCount(couponVOList, couponVOList.size());
    }

    @ApiOperation(value="根据优惠券id查询优惠券", notes="根据优惠券id查询优惠券")
    @ApiResponses({
            @ApiResponse(code = 200, response = CouponVOResponse.class, message = "根据优惠券id查询优惠券")
    })
    @GetMapping(value="/coupon/{couponId}", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseEntity<?> coupon(HttpServletRequest request,
                                        @PathVariable("couponId") @ApiParam(value="优惠券的主键Id", required=true) String couponId) {

        String userId = (String) request.getAttribute("userId");
        CouponEntity couponEntity = couponService.getCouponById(couponId);
        CouponVO couponVO = new CouponVO();
        if(couponEntity != null){
            BeanUtils.copyProperties(couponEntity, couponVO);
        }
        return CouponVOResponse.okWithData(couponVO);
    }
}
